<%@page import="MianCalPak.Homeservlet"%>
<%@page import="DB.DBProvider"%>
<%@ page language="java" contentType="text/html; charset=windows-1255"
    pageEncoding="windows-1255"%>
<%@ page import="java.util.*, java.io.*,java.sql.ResultSet" %>
<% 
  String admin = (String)request.getSession().getAttribute("administrator");
	String user = (String)request.getSession().getAttribute("username");
  if (null == admin){
	   response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
  }
  
  String number = request.getParameter("number");
  String query = request.getParameter("que");
  String Groupnum = request.getParameter("gro");
  String Creditpoints = request.getParameter("cre");
  String Available = request.getParameter("ava");
  boolean bAvailable = ((Available != null) && (Available.equals("yes")));
  
  Set<String> RelevantCourseNames = new	 HashSet<String>();
  Set<String> LessRelevantCourse = new	 HashSet<String>();
  int count = 0;
  ArrayList<String> temptitle = new ArrayList<String>(); 
  ArrayList<String> tempdesc = new ArrayList<String>(); 
  ArrayList<String> tempcred = new ArrayList<String>(); 
  ArrayList<String> tempcap = new ArrayList<String>(); 
  ArrayList<String> tempgroup = new ArrayList<String>();
  int count2 = 0;
  ArrayList<String> temptitle2 = new ArrayList<String>(); 
  ArrayList<String> tempdesc2 = new ArrayList<String>(); 
  ArrayList<String> tempcred2 = new ArrayList<String>(); 
  ArrayList<String> tempcap2 = new ArrayList<String>(); 
  ArrayList<String> tempgroup2 = new ArrayList<String>(); 
  
  
  int start = 0;
  if (null != number)
  {
	   start = Integer.parseInt(number);
  }
  
  int iGroupnum = -1;
  if ((null != Groupnum) && (!Groupnum.equals("")))
  {
	  iGroupnum = Integer.parseInt(Groupnum);
  } 
  
  int iCredit = -1;
  if ((null != Creditpoints) && (!Creditpoints.equals("")))
  {
	  iCredit = Integer.parseInt(Creditpoints);
  }
  String[] keywords = query.split(" ");
  
  Properties p = new Properties();
  p.load(getServletContext().getResourceAsStream("/WEB-INF/config"));
  ResultSet res;
  
  for(String ks : keywords)
  {
	  res = (new DBProvider(p)).searchCourseByfield("course_title",ks,start);
	  while (res.next())
	  {
			String title = res.getString("course_title");
			RelevantCourseNames.add(title);
			  temptitle.add(count,res.getString("course_title"));
			  String des =res.getString("description");
			  des = des.substring(0,Math.min(50,des.length()));
			  des += "...";
			  tempdesc.add(count,des);
			  tempcred.add(count,Integer.toString(res.getInt("number_credit_points")));
			  tempcap.add(count,Integer.toString(res.getInt("course_capacity")));
			  tempgroup.add(count,Integer.toString(res.getInt("group_number")));
			  count++;
	  }
	  
  }

  res = (new DBProvider(p)).fullTextSearch(query,start);


  
%>

<script type="text/javascript">


	function del_couse2(title)
	{
		var t;
		t = encodeURIComponent(title);
		$('#content').load("/Calendars/Homeservlet?op=delcourse&title=" +t,function(response, status, xhr) {
			  if (status == "error") {
				    var msg = "Sorry but there was an error: ";
				    $("#page").html("<br />" +"<h1>" + msg + xhr.status + " " + xhr.statusText +"</h1>" );
				  }
				});
		return true;
	}
	function reg_course2(title)
	{
		var t;
		t = encodeURIComponent(title);
		$('#content').load("/Calendars/Homeservlet?op=coursespage&title=" +t,function(response, status, xhr) {
			  if (status == "error") {
				    var msg = "Sorry but there was an error: ";
				    $("#page").html("<br />" +"<h1>" + msg + xhr.status + " " + xhr.statusText +"</h1>" );
				  }
				});
		return true;
	}
	
</script>
<div id="response">
</div>

<div class="post"">
<h1 class="title">Results</h1>
<%   
   if ((!res.next()) && RelevantCourseNames.isEmpty()){
   
	  %>
	  
   
	<h1> There aren't any courses to display</h1>
	   
<% } else 
	{
	res.beforeFirst();
	  while (res.next())
	  {
			  temptitle2.add(count2,res.getString("course_title"));
			  String des2 =res.getString("description");
			  des2 = des2.substring(0,Math.min(50,des2.length()));
			  des2 += "...";
			  tempdesc2.add(count2,des2);
			  tempcred2.add(count2,Integer.toString(res.getInt("number_credit_points")));
			  tempcap2.add(count2,Integer.toString(res.getInt("course_capacity")));
			  tempgroup2.add(count2,Integer.toString(res.getInt("group_number")));
			  count2++;
	  }
	  res.close();
	%>
			<br/>
			<table class = "usersTable">
				<tr>
				<th>Title</th>
				<th>Description</th>
				<th>Credit points</th>
				<th>Capacity</th>
				<th>Group number</th>
				<th></th>
				
				<% 
					 if (admin.equals("true")) {
  					 %>
    					<th></th>
    				<% } %>
			</tr>
<%	
		for(int j = 0; j<count2 ; j++)
		{
			String title = temptitle2.get(j);
			
			if(RelevantCourseNames.contains(title))
			{
				RelevantCourseNames.remove(title);
				
				if((iGroupnum != -1) && (iGroupnum != Integer.parseInt(tempgroup2.get(j))) )
				{
					continue;
				}
				if((iCredit != -1) && (iCredit != Integer.parseInt(tempcred2.get(j))) )
				{
					continue;
				}
				if(bAvailable)
				{
					int[] temparr = new int[2];
					ArrayList<String> reasons = new ArrayList<String>(); 
					reasons = Homeservlet.canRegister((new DBProvider(p)),user,title
											,Integer.parseInt(tempcap2.get(j)),
											Integer.parseInt(tempgroup2.get(j)),temparr);
					if(!reasons.isEmpty())
					{
						continue;
					}
					
					
				}
%>			
		
				<tr>
					<td><%=temptitle2.get(j)%></td>
					<td><%=tempdesc2.get(j)%></td>
					<td><%=tempcred2.get(j)%></td>
					<td><%=tempcap2.get(j)%></td>
					<td><%=tempgroup2.get(j)%></td>
					<td><button onclick="reg_course2(<%="'" + temptitle2.get(j) + "'"%>)">Course Page</button></td>
					<% 
					 if (admin.equals("true")) {
  					 %>
    					<td><button onclick="del_couse2(<%="'" + temptitle2.get(j) + "'" %>)">Delete course</button></td>
    				<% } %>
				</tr>
<%
			}
			else	
			{
				LessRelevantCourse.add(title);
			}
		}
	
		for(int j = 0; j<count2 ; j++)
		{
			String title = temptitle2.get(j);
			
			if(LessRelevantCourse.contains(title))
			{
				if((iGroupnum != -1) && (iGroupnum != Integer.parseInt(tempgroup2.get(j))) )
				{
					continue;
				}
				if((iCredit != -1) && (iCredit != Integer.parseInt(tempcred2.get(j))) )
				{
					continue;
				}
				if(bAvailable)
				{
					int[] temparr = new int[2];
					ArrayList<String> reasons = new ArrayList<String>(); 
					reasons = Homeservlet.canRegister((new DBProvider(p)),user,title
											,Integer.parseInt(tempcap2.get(j)),
											Integer.parseInt(tempgroup2.get(j)),temparr);
					if(!reasons.isEmpty())
					{
						continue;
					}
					
					
				}
%>			
		
				<tr>
					<td><%=temptitle2.get(j)%></td>
					<td><%=tempdesc2.get(j)%></td>
					<td><%=tempcred2.get(j)%></td>
					<td><%=tempcap2.get(j)%></td>
					<td><%=tempgroup2.get(j)%></td>
					<td><button onclick="reg_course2(<%="'" + temptitle2.get(j) + "'"%>)">Course Page</button></td>
					<% 
					 if (admin.equals("true")) {
  					 %>
    					<td><button onclick="del_couse2(<%="'" + temptitle2.get(j) + "'" %>)">Delete course</button></td>
    				<% } %>
				</tr>
<%
		     }
		}

			for(int i = 0; i<count ; i++)
			{
				if(RelevantCourseNames.contains(temptitle.get(i)))
				{
					
					if((iGroupnum != -1) && (iGroupnum != Integer.parseInt(tempgroup.get(i))) )
					{
						continue;
					}
					if((iCredit != -1) && (iCredit != Integer.parseInt(tempcred.get(i))) )
					{
						continue;
					}
					if(bAvailable)
					{
						int[] temparr = new int[2];
						ArrayList<String> reasons = new ArrayList<String>(); 
						reasons = Homeservlet.canRegister((new DBProvider(p)),user,temptitle.get(i)
												,Integer.parseInt(tempcap.get(i)),
												Integer.parseInt(tempgroup.get(i)),temparr);
						if(!reasons.isEmpty())
						{
							continue;
						}
						
						
					}
									
					
%>	
		  
				<tr>
					<td><%=temptitle.get(i)%></td>
					<td><%=tempdesc.get(i)%></td>
					<td><%=tempcred.get(i)%></td>
					<td><%=tempcap.get(i)%></td>
					<td><%=tempgroup.get(i)%></td>
					<td><button onclick="reg_course2(<%="'" + temptitle.get(i) + "'"%>)">Course Page</button></td>
					<% 
					 if (admin.equals("true")) {
  					 %>
    					<td><button onclick="del_couse2(<%="'" + temptitle.get(i) + "'" %>)">Delete course</button></td>
    				<% } %>
				</tr>





			
<%					
			}
		}
	}

%>
		</table>
		<br/>
	</div>
